<?php

namespace app\model;

use think\facade\Db;

/**
 * tms账单管理
 */
class XlBillManagementModel
{
    public static function lists($param): array
    {
        $userIds = getScopeView($param['uid']);
        if ($userIds != 0) {
            $where[] = ['management.uid', 'in', $userIds];
        }
        $where[] = ['management.car_id', '=', $param['car_id']];
        $where[] = [
            'management.partner_id', '=', $param['partner_id']
        ];
        $where[] = ['management.status', '<', 3];
        if (!empty($param['car_plate_number'])) {
            $where[] = ['management.car_plate_number', 'like', '%' . $param['car_plate_number'] . '%'];
        }
        if (!empty($param['type'])) {
            $where[] = ['management.type', '=', $param['type']];
        }
        if (!empty($param['start_time']) && !empty($param['end_time'])) {
            $where[] = [
                'management.add_time', '>', $param['start_time'],
            ];
            $where[] = [
                'management.add_time', '<', $param['end_time'],
            ];
        }
        if ($param['is_export'] == 1) {
            $param['limit'] = Db::table('xl_bill_management')->alias('management')->where([$where])->count();
        }
        $list = Db::table('xl_bill_management')->alias('management')
            ->where([$where])
            ->limit(($param['page'] - 1) * $param['limit'], $param['limit'])
            ->order('id', 'desc')
            ->field('management.*')->select()->toArray();
        $count = Db::table('xl_bill_management')->alias('management')->where([$where])->count();
        return ['list' => $list, 'count' => $count];
    }

    /**
     * 新增
     * @return void
     */
    public static function add($data)
    {
        return Db::table('xl_bill_management')->insert($data);
    }

    /**
     * 修改
     * @return int
     */
    public static function edit($data, $id)
    {
        return Db::table('xl_bill_management')->where('id', '=', $id)->update($data);
    }

    /**
     * 删除
     * @return void
     */
    public static function del($id)
    {
        return Db::table('xl_bill_management')->where('id', '=', $id)->update(['status' => 3, 'update_time' => getNowDateTime()]);
    }

    public static function info($id)
    {
        return Db::table('xl_bill_management')->where('id', '=', $id)->find();
    }
}
